//
//  NewFeatureController.m
//  weibo
//
//  Created by qiuming on 15/12/24.
//  Copyright © 2015年 qiuming. All rights reserved.
//

#import "NewFeatureController.h"
#import "NewFeatureCell.h"

#define imageCount 4

@interface NewFeatureController ()

@property (strong, nonatomic) UIPageControl *pageControl;

@end


NSString *const newFeatureCellReusedID = @"newFeatureCellReusedID";

@implementation NewFeatureController

// 设置flowLayout
- (instancetype)init {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = [UIScreen mainScreen].bounds.size;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    return [super initWithCollectionViewLayout:flowLayout];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    
    // 添加分页控件
    [self addPageControl];
    
    [self.collectionView registerClass:[NewFeatureCell class] forCellWithReuseIdentifier:newFeatureCellReusedID];
//    self.collectionView.delegate = self;
//    self.collectionView.dataSource = self;
    
}

- (void)addPageControl {
    // 添加控件
    [self.view addSubview:self.pageControl];
    
    self.pageControl.currentPage = 0;
    self.pageControl.numberOfPages = imageCount;
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view.mas_bottom).offset(-80);
    }];
}

// UICollectionViewDateSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return imageCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    NewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:newFeatureCellReusedID forIndexPath:indexPath];
    
    NSString *imageName = [NSString stringWithFormat:@"new_feature_%ld", indexPath.item + 1];
    cell.imageName = imageName;
    
    // 当滚到最后一页显示分享和进入主页按钮
    if (indexPath.item == imageCount - 1) {
        [cell setSharedBtnAndStartBtnHidden:NO];
        
    } else {
        [cell setSharedBtnAndStartBtnHidden:YES];
    }
    return cell;
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger page = roundf(scrollView.contentOffset.x / scrollView.width);
    self.pageControl.currentPage = page;
}



- (UIPageControl *)pageControl {
    if (_pageControl == nil) {
        _pageControl = [[UIPageControl alloc]init];
        _pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
        _pageControl.pageIndicatorTintColor = [UIColor blackColor];
        _pageControl.userInteractionEnabled = NO;
    }
    return _pageControl;
}


@end
